/*
 * Copyright © Jack魏 2022 - 2022, All Rights Reserved.
 */

package cn.jack1996.yiyi.service.sys;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jack1996.yiyi.bean.SysUser;
import cn.jack1996.yiyi.mapper.sys.SysUserMapper;

import cn.hutool.core.util.IdUtil;
import lombok.extern.log4j.Log4j2;

/**
 * 系统用户服务类
 *
 * @author Jack魏
 * @since 2022-11-12
 */
@Log4j2
@Service
public class SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 注册用户
     *
     * @param sysUser 用户信息
     * @return 注册用户
     */
    public SysUser register(SysUser sysUser) {
        // 如果已经有用户名存在，注册就失败
        SysUser user = sysUserMapper.selectByAccount(sysUser.getAccount());
        if (user != null) {
            return null;
        }

        sysUser.setId(IdUtil.fastSimpleUUID());
        sysUser.setUpdateTime(new Date());
        sysUser.setCreateTime(new Date());
        int insertNum = sysUserMapper.insert(sysUser);
        if (insertNum > 0) {
            log.info("{}用户注册成功", sysUser.getAccount());
        } else {
            log.error("插入用户信息错误！");
            return null;
        }
        return sysUser;
    }

    /**
     * 根据登录名，获取用户信息
     *
     * @param account 登录名
     * @return 用户信息
     */
    public SysUser getByAccount(String account) {
        return sysUserMapper.selectByAccount(account);
    }

    /**
     * 根据用户id获取用户数据
     *
     * @param userId 用户id
     * @return 用户信息
     */
    public SysUser getById(String userId) {
        return sysUserMapper.selectById(userId);
    }
}
